package com.jsle.financial.manager;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jsle.financial.dao.WorkflowDao;
import com.jsle.financial.domain.Node;
import com.jsle.financial.domain.Workflow;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Result;

public class WorkflowManagerImpl implements WorkflowManager{
	private Log log = LogFactory.getLog(WorkflowManagerImpl.class);
	@Resource
	private WorkflowDao workflowDao;
	
	@SuppressWarnings("unused")
	private PlatformTransactionManager transactionManager;
	
	@Transactional(value="transactionManager",readOnly = false, propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	public Result saveAmountWorkflow(Workflow workflow){
		Result result = new Result();
		try {
			result = workflowDao.saveAmountWorkflow(workflow);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("WorkflowManager error## "+ e.getMessage());
		}
		return result;
	}
	@Transactional(value="transactionManager",readOnly = false, propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	public Result saveAmountNode(Node node){
		Result result = new Result();
		try {
			result = workflowDao.saveAmountNode(node);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("saveAmountNode error## "+ e.getMessage());
		}
		return result;
	}
	@Transactional(value="transactionManager",readOnly = false, propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	public Result updateWorkflow(Workflow workflow){
		Result result = new Result();
		try {
			result = workflowDao.updateWorkflow(workflow);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("saveAmountNode error## "+ e.getMessage());
		}
		return result;
	}
	@Transactional(value="transactionManager",readOnly = false, propagation = Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	public Result updateNode(Node node){
		Result result = new Result();
		try {
			result = workflowDao.updateNode(node);
		} catch (Exception e) {
			result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE, "User"));
			log.error("saveAmountNode error## "+ e.getMessage());
		}
		return result;
	}
}
